<?php
require_once(dirname(__FILE__).'/../../WildPack/WildPack.php');
$wp = new WildPack();
$wp->using('util');
$wp->using('base.wplog');

class TestWpFileLog extends WpTestSuite
{
  
  /**
   * Preparation
   */
  public function prepare()
  {
    $path1 = dirname(__FILE__).'/filelog1.txt';
    $path2 = dirname(__FILE__).'/filelog2.txt';
    
    if (file_exists($path1)) {
      try {
        unlink($path1);
      } catch(Exception $e) {
        $this->addMessage('Warning: An error occurred, deleting the old logfile (filelog1.txt).');
      }
      $this->addMessage('Old log file has successfully deleted.');
    } else {
     $this->addMessage('No old log file found. Skipped cleanup.'); 
    }
    
    $this->addMessage('Realtime log will be written to: '.$path1);
    
    if (file_exists($path2)) {
      try {
        unlink($path2);
      } catch(Exception $e) {
        $this->addMessage('Warning: An error occurred, deleting the old logfile (filelog2.txt).');
      }
      $this->addMessage('Old log file has successfully deleted.');
    } else {
     $this->addMessage('No old log file found. Skipped cleanup.'); 
    }
    
    $this->addMessage('Cache log will be written to: '.$path2);
    return true;
  }
  
  /**
   * Test
   */
  public function test()
  {
    $path1 = dirname(__FILE__).'/filelog1.txt';
    $path2 = dirname(__FILE__).'/filelog2.txt';
    
    //-- Realtime tests --
    $l = new WpFileLog($path1, true);
    $l->logError('Location', 'Admin', 'Test1', 'Body1', '1', 1);
    $l->logError('Location2', 'Admin', 'Test2', 'Body2', '1', 1);
    $l->logMessage('Location2', 'Admin', 'Message1', 'BodyMessage', '1', 1);
    $l->logAction('Location2', 'Admin', 'Action1', 'BodyAction', '1', 1);
    $l->logAudit('Location2', 'Admin', 'Audit1', 'BodyAudit', '1');
    
    //-- Basics --
    $chk = file_get_contents($path1);
    $this->addResult('Log content not null', false, is_null($chk), '===');
    $this->addResult('Log content not empty', true, $chk !== '', '===');
    $achk = explode("\n", $chk);
    $this->addResult('Number of entries', 5, $achk, 'count');
    
    foreach ($achk as $chke) {
      $achke = explode("\t", $chke); 
      $this->addResult('Level', '1', $achke[1], '===');
      $this->addResult('User', 'Admin', $achke[6], '===');
    }
    
    //-- Location --
    $fdcheck = explode("\t", $achk[0]);
    $this->addResult('Location 1', 'Location', $fdcheck[7], '===');
    
    $fdcheck = explode("\t", $achk[1]);
    $this->addResult('Location 2', 'Location2', $fdcheck[7], '===');
    
    $fdcheck = explode("\t", $achk[2]);
    $this->addResult('Location 3', 'Location2', $fdcheck[7], '===');
    
    //-- Type --
    $fdcheck = explode("\t", $achk[0]);
    $this->addResult('Check error type', 'error', $fdcheck[2], '===');
    
    $fdcheck = explode("\t", $achk[2]);
    $this->addResult('Check message type', 'message', $fdcheck[2], '===');
    
    $fdcheck = explode("\t", $achk[3]);
    $this->addResult('Check action type', 'action', $fdcheck[2], '===');
    
    $fdcheck = explode("\t", $achk[4]);
    $this->addResult('Check audit type', 'audit', $fdcheck[2], '===');
    
    //-- Title --
    $fdcheck = explode("\t", $achk[0]);
    $this->addResult('Check title', 'Test1', $fdcheck[4], '===');
    
    $fdcheck = explode("\t", $achk[1]);
    $this->addResult('Check title', 'Test2', $fdcheck[4], '===');
    
    $fdcheck = explode("\t", $achk[2]);
    $this->addResult('Check title', 'Message1', $fdcheck[4], '===');
    
    $fdcheck = explode("\t", $achk[3]);
    $this->addResult('Check title', 'Action1', $fdcheck[4], '===');
    
    $fdcheck = explode("\t", $achk[4]);
    $this->addResult('Check title', 'Audit1', $fdcheck[4], '===');
    
    //-- Body --
    $fdcheck = explode("\t", $achk[0]);
    $this->addResult('Check body', 'Body1', $fdcheck[5], '===');
    
    $fdcheck = explode("\t", $achk[1]);
    $this->addResult('Check body', 'Body2', $fdcheck[5], '===');
    
    $fdcheck = explode("\t", $achk[2]);
    $this->addResult('Check body', 'BodyMessage', $fdcheck[5], '===');
    
    $fdcheck = explode("\t", $achk[3]);
    $this->addResult('Check body', 'BodyAction', $fdcheck[5], '===');
    
    $fdcheck = explode("\t", $achk[4]);
    $this->addResult('Check body', 'BodyAudit', $fdcheck[5], '===');
    
    //-- Cache tests --
    $l = new WpFileLog($path2, false);
    $l->logError('Location', 'Admin', 'Test1', 'Body1', '1', 1);
    $l->logError('Location2', 'Admin', 'Test2', 'Body2', '1', 1);
    $l->logMessage('Location2', 'Admin', 'Message1', 'BodyMessage', '1', 1);
    $l->logAction('Location2', 'Admin', 'Action1', 'BodyAction', '1', 1);
    $l->logAudit('Location2', 'Admin', 'Audit1', 'BodyAudit', '1');
    
    //-- Basics --
    $chk = file_get_contents($path2);
    $this->addResult('Log content false', false, $chk, '===');
    $this->addMessage('Call destructor');
    $l->__destruct();
    
    //-- Basics --
    $chk = file_get_contents($path1);
    $this->addResult('Log content not null', false, is_null($chk), '===');
    $this->addResult('Log content not empty', true, $chk !== '', '===');
    $achk = explode("\n", $chk);
    $this->addResult('Number of entries', 5, $achk, 'count');
    
    foreach ($achk as $chke) {
      $achke = explode("\t", $chke); 
      $this->addResult('Level', '1', $achke[1], '===');
      $this->addResult('User', 'Admin', $achke[6], '===');
    }
    
    //-- Location --
    $fdcheck = explode("\t", $achk[0]);
    $this->addResult('Location 1', 'Location', $fdcheck[7], '===');
    
    $fdcheck = explode("\t", $achk[1]);
    $this->addResult('Location 2', 'Location2', $fdcheck[7], '===');
    
    $fdcheck = explode("\t", $achk[2]);
    $this->addResult('Location 3', 'Location2', $fdcheck[7], '===');
    
    //-- Type --
    $fdcheck = explode("\t", $achk[0]);
    $this->addResult('Check error type', 'error', $fdcheck[2], '===');
    
    $fdcheck = explode("\t", $achk[2]);
    $this->addResult('Check message type', 'message', $fdcheck[2], '===');
    
    $fdcheck = explode("\t", $achk[3]);
    $this->addResult('Check action type', 'action', $fdcheck[2], '===');
    
    $fdcheck = explode("\t", $achk[4]);
    $this->addResult('Check audit type', 'audit', $fdcheck[2], '===');
    
    //-- Title --
    $fdcheck = explode("\t", $achk[0]);
    $this->addResult('Check title', 'Test1', $fdcheck[4], '===');
    
    $fdcheck = explode("\t", $achk[1]);
    $this->addResult('Check title', 'Test2', $fdcheck[4], '===');
    
    $fdcheck = explode("\t", $achk[2]);
    $this->addResult('Check title', 'Message1', $fdcheck[4], '===');
    
    $fdcheck = explode("\t", $achk[3]);
    $this->addResult('Check title', 'Action1', $fdcheck[4], '===');
    
    $fdcheck = explode("\t", $achk[4]);
    $this->addResult('Check title', 'Audit1', $fdcheck[4], '===');
    
    //-- Body --
    $fdcheck = explode("\t", $achk[0]);
    $this->addResult('Check body', 'Body1', $fdcheck[5], '===');
    
    $fdcheck = explode("\t", $achk[1]);
    $this->addResult('Check body', 'Body2', $fdcheck[5], '===');
    
    $fdcheck = explode("\t", $achk[2]);
    $this->addResult('Check body', 'BodyMessage', $fdcheck[5], '===');
    
    $fdcheck = explode("\t", $achk[3]);
    $this->addResult('Check body', 'BodyAction', $fdcheck[5], '===');
    
    $fdcheck = explode("\t", $achk[4]);
    $this->addResult('Check body', 'BodyAudit', $fdcheck[5], '===');
    
  }
  
  /**
   * Cleanup
   */
  public function cleanup()
  {
    $path1 = dirname(__FILE__).'/filelog1.txt';
    $path2 = dirname(__FILE__).'/filelog2.txt';
    
    if (file_exists($path1)) {
      try {
        unlink($path1);
      } catch(Exception $e) {
        $this->addMessage('Warning: An error occurred, deleting the old logfile (filelog1.txt).');
      }
      $this->addMessage('Old log file has successfully deleted.');
    } else {
     $this->addMessage('No old log file found. Skipped cleanup.'); 
    }
    
    if (file_exists($path2)) {
      try {
        unlink($path2);
      } catch(Exception $e) {
        $this->addMessage('Warning: An error occurred, deleting the old logfile (filelog2.txt).');
      }
      $this->addMessage('Old log file has successfully deleted.');
    } else {
     $this->addMessage('No old log file found. Skipped cleanup.'); 
    }
  }
  
  
}

$t = new TestWpFileLog();
$t->start('WpFileLog');
